package com.sip.comadapter.executor;

import java.util.Map;

import com.sip.comadapter.common.BaseExecutor;
import com.sip.comadapter.executor.common.Executor;
import com.sip.comserver.communication.CommunicationMessage;
import com.sip.dbadapter.dao.domain.ApiDefinition;
import com.sip.dbadapter.dao.domain.ScenarioRecord;
import com.sip.dbadapter.dao.interfaces.ApiDao;
import com.sip.dbadapter.dao.interfaces.RestServiceDao;
import com.sip.dbadapter.dao.interfaces.ScenarioRecordSaveDao;
import com.sip.dbadapter.dao.utility.DaoLoadFactory;
import com.sip.dbadapter.dao.utility.DaoNameConstant;
import com.sip.dbadapter.domain.registry.DomainRegistry;
import com.sip.executionadapter.manager.JerseyExecutionManager;

public class RESTServiceExecutor extends BaseExecutor implements Executor {

    public CommunicationMessage execute(final CommunicationMessage message) throws Exception {

        // get the method url for api id
        final RestServiceDao restServiceDao = (RestServiceDao) DaoLoadFactory.getDao(DaoNameConstant.GET_REST_URL);

        final Map<String, Object> domainMap = (Map<String, Object>) message.getObject();
        final ApiDefinition apiDefinitionDomainObj = (ApiDefinition) domainMap.get(DomainRegistry.API_DEFINITION);

        final Long apiId = apiDefinitionDomainObj.getApiId();
        
        if("".equals(apiDefinitionDomainObj.getPackageHierachy()) || apiDefinitionDomainObj.getPackageHierachy()==null){
        	final ApiDao apiDao = (ApiDao) DaoLoadFactory.getDao(DaoNameConstant.API_DAO);
        	final ApiDefinition apiDefinition = (ApiDefinition) apiDao.findById(apiId);
        	apiDefinitionDomainObj.setPackageHierachy(apiDefinition.getPackageHierachy());
        }
        final JerseyExecutionManager jerseyManager = JerseyExecutionManager.createInstance();
        jerseyManager.invoke(domainMap);

        final ScenarioRecord scenarioRecordDomainObj = (ScenarioRecord) domainMap.get(DomainRegistry.SCENARIO_RECORD);

        final ApiDao apiDao = (ApiDao) DaoLoadFactory.getDao(DaoNameConstant.API_DAO);
        final ScenarioRecordSaveDao scenarioRecordeDao = (ScenarioRecordSaveDao) DaoLoadFactory.getDao(DaoNameConstant.SCENARIO_RECORD_DAO);
        final ApiDefinition apiDefinition = (ApiDefinition) apiDao.findById(apiId);

        scenarioRecordDomainObj.setApiDefinitionScenarioRecord(apiDefinition);
        scenarioRecordDomainObj.setExecuteMode("SINGLE");
        scenarioRecordeDao.save(scenarioRecordDomainObj);

        message.setObject(domainMap);

        return message;
    }

    /*
     * public static void main(String args[]) throws Exception { RESTServiceExecutor res = new RESTServiceExecutor();
     * ScenarioRecordSaveDao scenarioRecordDao = res.loadScenarioRecordDao(); ApiDao apiDao =
     * res.loadApiDefinitionDao(); ApiDefinition apiDefinition = (ApiDefinition) apiDao.findById(1l); ScenarioRecord
     * scenarioRecordObj = new ScenarioRecord(); scenarioRecordObj.setApiDefinitionScenarioRecord(apiDefinition);
     * scenarioRecordDao.update(scenarioRecordObj); }
     */

}
